<template>
  <div id="home">
    <div id="home-nav-bar">
      <nav-bar >
        <template v-slot:center>
          <div>购物街</div>
        </template>
      </nav-bar>
    </div>
    <tab-control ref="tabControl2" v-show="isTabFixed" class="fixed" @itemClick="tabClick"
                 :titles="['流行', '新款', '精选']"></tab-control>
    <scroll id="home-content"
    ref="scroll"
    @scroll="contentScroll"
    @pullingUp="loadMore" 
    :probeType="2" 
    :pullUpload="true">
      <div>
        <home-swiper ref="hSwiper" :banners="banners" @swiperImageLoad="swiperImageLoad"></home-swiper>
        <feature-view :features="recommends"></feature-view>
        <recommend-view></recommend-view>
        <tab-control ref="tabControl" :titles="['流行', '新款', '精选']" @itemClick="tabClick"></tab-control>
        <goods-list :goods-list="showGoodsList"></goods-list>
      </div>
    </scroll>
    <back-top @backTop="backTop" class="back-top" v-show="showBackTop">
      <img src="~assets/img/common/top.png" alt="">
    </back-top>
  </div>
</template>

<script>
  import NavBar from 'common/navbar/NavBar'
  import {getHomeMultidata, getHomeData, RECOMMEND, BANNER} from "network/home";
  import {NEW, POP, SELL, BACKTOP_DISTANCE} from "@/common/const";
  import HomeSwiper from './childComps/HomeSwiper'
  import FeatureView from './childComps/FeatureView'
  import RecommendView from './childComps/RecommendView'
  import TabControl from '@/components/common/tabControl/TabControl'
  import GoodsList from './childComps/GoodsList'
  import Scroll from '@/components/common/scroll/Scroll'
  import BackTop from './childComps/BackTop'
  
  export default {
    name: "Home",
    components:{
      NavBar,
      HomeSwiper,
      FeatureView,
      RecommendView,
      TabControl,
      GoodsList,
      Scroll,
      BackTop
    },
    data() {
      return {
        banners:[],
        recommends:[],
        goodsList:{
          'pop': {page: 1, list:[]},
          'new': {page: 1, list:[]},
          'sell': {page: 1, list:[]},
        },
        currentType:POP,
        showBackTop: false,
        tabOffsetTop: 0,
        isTabFixed: false,
      }
    },
    computed:{
      showGoodsList(){
        return this.goodsList[this.currentType].list
      }
    },
    created() {
      this.getMultiData()
      // 2.请求商品数据
      this.getHomeProducts(POP)
      this.getHomeProducts(NEW)
      this.getHomeProducts(SELL)
    },
    activated: function () {
      console.log('home activated');
      this.$refs.hSwiper.startTimer()
    },
    deactivated: function () {
      console.log('home deactivated');
      this.$refs.hSwiper.stopTimer()
    },
    methods:{
      getMultiData(){
        getHomeMultidata().then((result) => {
          this.banners = result.data[BANNER].list
          this.recommends = result.data[RECOMMEND].list
        }).catch((err) => {
          console.log(err);
        });
      },
      getHomeProducts(type) {
        getHomeData(type, this.goodsList[type].page).then(res => {
          const goodsList = res.list;
          this.goodsList[type].list.push(...goodsList)
          this.goodsList[type].page += 1
          this.$refs.scroll.finishPullUp()
        }).catch((err) => {
          console.log(err);
          this.$refs.scroll.finishPullUp()
        })
      },
      tabClick(index){
        switch (index) {
          case 0:
            this.currentType = POP
            break;
          case 1:
            this.currentType = NEW
            break;
          case 2:
            this.currentType = SELL
            break;
          default:
            break;
        }
        this.$refs.tabControl.currentIndex = index
        this.$refs.tabControl2.currentIndex = index
      },
      loadMore() {
		    this.getHomeProducts(this.currentType)
      },
      contentScroll(position) {
        // 决定tabFixed是否显示
        this.isTabFixed = position.y < -this.tabOffsetTop
        //决定backTop是否显示
        this.showBackTop = position.y < -BACKTOP_DISTANCE
      },
      backTop() {
        this.$refs.scroll.scrollTo(0, 0, 300)
      },
      swiperImageLoad(){
        this.$nextTick(()=>{
            this.tabOffsetTop = this.$refs.tabControl.$el.offsetTop
            console.log("tabOffsetTop = ", this.tabOffsetTop);
        });
      },
    }
  }

</script>

<style scoped>
  #home {
    height: 100vh;
    position: relative;
  }
  #home-nav-bar {
    background-color: var(--color-tint);
    font-weight: 700;
    color: #fff;
    position: relative;
    z-index: 5;
    width: 100%;
  }
  #home-content {
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
    overflow: scroll;
  }
  .back-top {
    position: fixed;
    right: 10px;
    bottom: 60px;
  }
</style>